import { isString } from "@/utils"
import store from '@/store'
const state = {
  socket:null,
  eventMsgs:[],
  sendMsgQueue:[],
}

const mutations = {
  ADD_EVENT_MSG: (state, msg) => {
    state.eventMsgs.push(msg)
  },
  WS_INIT:(state,ws)=>{
    state.socket = ws;
  },
  WS_SEND_MSG(state,data){
    data.local_id = new Date().getTime() + '' + parseInt(Math.random() * 89999 + 10000)
    data.platform_app_id = store.getters.appInfo.platform_app_id;
    state.socket.send(JSON.stringify(data));
  }
}

const actions = {
  addEventMsg({ commit }, msg){
    commit('ADD_EVENT_MSG', msg);
  },
  send({commit},msg){
    state.socket.send(isString(msg) ? msg : JSON.stringify(msg));
  },
  sendCustServiceMsg({commit},data){
    data.type = 'CustServiceToUser';
    commit('WS_SEND_MSG',data);
  },
  initWS({commit},ws){
    commit('WS_INIT',ws);
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

